obj/gettable
	var is_for_sale=0
	DblClick()
		if(is_for_sale)
			var/mob/player/m = usr
			if(m.jewels < cost)
				m << "Sorry, you need [cost] jewels to buy [src]."
				return
			if(alert(m, "Would you like to buy [src] for [cost][amount>1 ? " [(x[amount])]" : ""]?","Buying [src]","Yes","No") == "Yes")
				for(var/obj/shop_related/player_shop/counter/c in loc)
					for(var/obj/shop_related/player_shop/money_claim_box/mcb in oview(10))
						if(mcb.owner_ckey == c.owner_ckey)
							mcb.add_money(cost)
							break
					c.change_ref(null)
					Move(m)
					m.jewels -= cost
					break
		else ..()

obj/shop_related
	player_shop

		var
			owner_ckey
			uid

		money_claim_box
			icon='objects.dmi'
			icon_state="cashbox"
			name = "Shop Management Box"
			var/money=0
			density=1

			proc/add_money(x)
				money += x
				Query("UPDATE `[sql_pstoretable]` SET cur_money=[money] WHERE uid='[uid]';")

			DblClick()
				var/mob/player/m = usr
				if(m.ckey != owner_ckey)
					m << "This is not your shop."
					return

				switch(alert(m, "What would you like to do? There is [money] jewels available to claim.","Shop Management","Close Shop","Claim Money","Cancel"))
					if("Close Shop")
						m.jewels += money
						for(var/obj/gettable/o in oview(10, src))
							if(o.is_for_sale)
								o.is_for_sale = 0
								Query("DELETE FROM `[sql_pstoretable]` WHERE uid='[o.uid]';")
								o.Move(m)

						for(var/obj/shop_related/player_shop/p in oview(10,src))
							del p

						del src

					if("Claim Money")
						m << "You got [money] jewels!"
						m.jewels += money
						money = 0

		counter
			layer=MOB_LAYER+3
			icon='BuildTwo.dmi'
			icon_state="box"
			density=1

			proc/change_ref(obj/o)

				if(!ref && o || ref==o)

					//insert
					if(istype(o, /obj/gettable/))
						var/obj/gettable/g = o
						if(!g.cost)
							ref = o
							return
						Query("INSERT INTO `[sql_pstoretable]` (type,x,y,z,cost,uid,amount,ckey) VALUES ('[o.type]',[x],[y],[z],[g.cost],'[g.uid]',[g.amount],'[owner_ckey]');")

					else if(istype(o, /obj/shop_related/player_shop/custom))
						var/obj/shop_related/player_shop/custom/g = o
						Query("INSERT INTO `[sql_pstoretable]` (type,x,y,z,cost,uid,iconFile,icon_state,ckey) VALUES ('[o.type]',[x],[y],[z],[g.cost],'[g.uid]','[g.iconFile]','[g.icon_state]','[owner_ckey]');")

				else if(ref && !o)
					Query("DELETE FROM `[sql_pstoretable]` WHERE uid='[ref:uid]';")
					if(istype(ref, /obj/shop_related/player_shop/custom))
						del ref

				ref = o

			var/obj/ref

			mouse_opacity=2

			RightClick(mob/player/caller)
				if(caller.ckey != owner_ckey)
					caller << "This is not your shop."
					return
				if(alert(caller, "Would you like to remove [ref] from the shop?","Shop Removal","Yes","No")=="Yes")
					if(istype(ref, /obj/gettable/))
						var/obj/gettable/g = ref
						g.layer = TURF_LAYER
						g.cost=0
						g.is_for_sale = 0
						g.Move(caller)
						change_ref(null)
						caller.update_contents()

					else
						change_ref(null)


			DblClick()
				var/mob/player/m = usr

				if(m.ckey != owner_ckey)

					if(istype(ref, /obj/shop_related/player_shop/custom))
						var/obj/shop_related/player_shop/custom/x = ref

						if(m.jewels < x.cost)
							m << "You don't have enough jewels to buy this. It costs [x.cost]."
							return

						if(alert(m, "Would you like to buy this icon for [x.cost]?","Buying an Icon","Yes","No")=="Yes")
							var/obj/shop_related/player_shop/money_claim_box/c = find_center()
							m << ftp(x.icon)
							m.jewels -= x.cost
							c.add_money(x.cost)
							return

				if(ref)
					m << "This counter is full."
					return

				if(alert(m, "Would you like to put an icon here for sale?","Sell an Icon","Yes","No") == "Yes")
					var/icon/i = input("Choose an icon file.") as null|icon
					if(!i) return
					var/_icon_state = input(m,"What icon_state?") in icon_states(i)
					var/_iconFile="icons_custom/[m.ckey]/[i]"
					fcopy(i,_iconFile)

					var/x = input(m,"For what cost will this be sold?") as num
					x = safe_num(x)

					var/obj/shop_related/player_shop/custom/c = new(loc)
					c.layer = layer+2
					c.iconFile = _iconFile
					c.icon_state = _icon_state
					c.cost = x
					c.name = c.icon_state

					change_ref(c)

			proc

				handle_object(mob/player/seller, obj/gettable/o)

					if(seller.ckey != owner_ckey)
						return

					if(ref)
						seller << "This counter is full."
						return

					if(o.amount>1)
						var/obj/gettable/stackable/s = o
						var/x = input(seller, "How many would you like to sell in this stack? Max: [o.amount]") as num
						x = min(abs(round(x)),o.amount)
						if(x==o.amount)
							o.Move(loc)
							change_ref(o)
						else if(x>=1)
							change_ref(new o.type(loc, x))
							s.remove(x)
					else
						o.Move(loc)
						change_ref(o)

					var/x = input(seller,"For what cost will [o] be sold?") as num
					x = safe_num(x)

					var/obj/gettable/z = ref

					z.layer = layer+2

					z.cost = x

					z.is_for_sale = 1

					change_ref(ref)

		custom
			mouse_opacity=0
			var
				iconFile
				cost

		proc/find_center()
			for(var/obj/shop_related/player_shop/money_claim_box/c in oview(15, src))
				if(c.owner_ckey == owner_ckey)
					return c


		New(loc, name, uiz)
			..()
			owner_ckey = name

			if(!uiz)
				uid=gen_uid(name)
				if(!istype(src,/obj/shop_related/player_shop/custom))
					Query("INSERT INTO `[sql_pstoretable]` (type,x,y,z,uid,ckey) VALUES ('[type]',[x],[y],[z],'[uid]','[owner_ckey]');")
			else
				uid = uiz

		Del()
			Query("DELETE FROM `[sql_pstoretable]` WHERE uid='[uid]';")
			..()

			//pstorevars = list("type"="","x"=0,"y"=0,"z"=0,"cost"=0,"iconFile"="","icon_state"="","cur_money"=0,"uid"="")

mob/player
	var/max_build_range=2
	verb
		set_up_shop()
			//preliminary checks
			for(var/obj/shop_related/player_shop/c in world)
				if(c.owner_ckey == ckey)
					src << "You already have a shop open. Please close it first."
					return

			for(var/obj/shop_related/player_shop/money_claim_box/c in oview(10, src))
				if(c)
					src << "There is a shop too close to this one. Please more farther away."
					return

			for(var/turf/t in oview(max_build_range, src))
				if(istype(t, /turf/unbuildable))
					src << "You cannot build in this area \[Unbuildable Area]."
					return
				for(var/atom/o in t)
					if(o == src)continue
					if(istype(o,/obj/builditems))
						var/obj/builditems/x = o
						if(x.builtowner != ckey)
							src << "You cannot build in this area \[Not Your Property]."
							return
					if(o.density)
						src << "You cannot build in this area \[Density Present]."
						return

			//actually makin' that shit, yo
			new /obj/shop_related/player_shop/money_claim_box(loc, ckey)

			for(var/turf/t in oview(max_build_range, src))
				if(get_dist(src, t) == max_build_range)
					if(get_dir(src, t) == EAST && t.y == y)
						continue
					new /obj/shop_related/player_shop/counter(t, ckey)
